﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;

namespace EntityFramework
{
    public class AuthorityManage:IAuthorityRepository
    {
        TrainingWorkshopEntities dbContext = new TrainingWorkshopEntities();
        //AuthorityManage auma = new AuthorityManage();
        public bool AddNewAuthority(BLL.Authority authorityBE)
        {
            EntityFramework.Authority authorityDE = Translator.AuthorityTranslator.TranslatetoAuthorityDE(authorityBE);
            dbContext.AddToAuthorities(authorityDE);
            dbContext.SaveChanges();
            return true;
        }

        public bool DeleteAuthority(int AuthorityId)
        {
            EntityFramework.Authority authorityDE = (from Authority a in dbContext.Authorities where a.AuthorityId == AuthorityId select a).SingleOrDefault<Authority>();
            dbContext.Authorities.DeleteObject(authorityDE);
            dbContext.SaveChanges();
            return true;
        }

        public bool UpdateAuthority(BLL.Authority authorityBE)
        {
            EntityFramework.Authority authorityDE = Translator.AuthorityTranslator.TranslatetoAuthorityDE(authorityBE);
            authorityDE = (from Authority a in dbContext.Authorities where a.AuthorityId == authorityDE.AuthorityId select a).SingleOrDefault<Authority>();
            authorityDE.AuthorityName = authorityBE.AuthorityName;
            authorityDE.LevelId = authorityBE.Level.LevelId;
           // authorityDE.AuthorityLevel.LevelId = authorityBE.Level.LevelId;
            dbContext.SaveChanges();
            return true;
        }
        
        public BLL.Authority GetAuthorityDetails(int authorityId)
        {
            EntityFramework.Authority authorityDE = (from Authority a in dbContext.Authorities where a.AuthorityId == authorityId select a).SingleOrDefault<Authority>();
            BLL.Authority authorityBE = Translator.AuthorityTranslator.TranslatetoAuthorityBE(authorityDE);
            return authorityBE;
        }

        public List<BLL.AuthorityLevel> GetAllAuthorityLevelName()
        {
            List<EntityFramework.AuthorityLevel> listDE = (from AuthorityLevel l in dbContext.AuthorityLevels select l).ToList<AuthorityLevel>();
            List<BLL.AuthorityLevel> authorityLevelListBE = new List<BLL.AuthorityLevel>();
            foreach (AuthorityLevel levelDE in listDE)
            {
                BLL.AuthorityLevel levelBE = Translator.AuthorityLeveltranslator.TranslatetoAuthorityLevelBE(levelDE);
                authorityLevelListBE.Add(levelBE);
            }
            return authorityLevelListBE;
        }

        public List<BLL.Authority> GetAuthorityAndLevel()
        {
            List<EntityFramework.Authority> listDE = (from a in dbContext.Authorities join l in dbContext.AuthorityLevels on a.AuthorityLevel.LevelId equals l.LevelId select a).ToList<Authority>();
            List<BLL.Authority> authorityAndLevelListBE=new List<BLL.Authority>();
            foreach (Authority authorityDE in listDE)
            {
                BLL.Authority authorityBE = Translator.AuthorityTranslator.TranslatetoAuthorityBE(authorityDE);
                authorityAndLevelListBE.Add(authorityBE);

            }
            return authorityAndLevelListBE;
        }
    }
}
